import Vue from "vue";
import Router from "vue-router";
import { asyncRouterMap, constantRouterMap } from "@/config/router.config";
import config from "@/config/defaultSettings";

Vue.use(Router);

// hack router push callback
// const originalPush = Router.prototype.push
// Router.prototype.push = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }

let routerList;
if (config.routerServe) {
  routerList = constantRouterMap;
} else {
  routerList = constantRouterMap.concat(asyncRouterMap);
}

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes: routerList
  // routes:constantRouterMap,
});

export function resetRouter() {
  // 获取原始路由数据
  // var options = router.options;
  // 用原始数据重新 new 一个路由
  var _VueRouter = new Router({ mode: "history", routes:constantRouterMap });
  // 用新路由的 matcher 替换旧路由的 matcher
  router.matcher = _VueRouter.matcher;
}

export default router